<?php

namespace app\admin\controller;

use app\admin\model\Building as BuildingModel;
use app\admin\model\BuildingRoomModel;
use app\admin\model\RoomBoardLogModel;
use app\admin\model\BillModel;
use app\admin\model\Vacant as VacantModel;
use app\common\controller\Backend;

/**
 * 空置率
 *
 * @icon fa fa-circle-o
 */
class Vacant extends Backend
{

    /**
     * Vacant模型对象
     * @var \app\admin\model\Vacant
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Vacant;
        $RoomBoardLogModel = new RoomBoardLogModel();
        $this->view->assign("typeList", $RoomBoardLogModel->getTypeList());
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = VacantModel::where($where)
                ->field('day,total_num,rent_num,rate,year, month, day')
                ->where('user_id',$this->auth->id)
                ->order('id', 'desc')
                    ->paginate($limit);

//            foreach ($list as $row) {
//
//                $row->getRelation('user')->visible(['nickname']);
//            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 查看
     */
    public function year()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = BillModel::where('user_id', $this->auth->id)
                ->where('type', 1)
                ->whereIn('status', [1, 2])
                ->field('year,sum(money) as total_money')
                ->group('year')
                ->paginate($limit)->each(function ($item) {
                    $pay = BillModel::where('user_id', $this->auth->id)
                        ->where('type', 1)
                        ->where('year', $item->year)
                        ->where('status', 2)
                        ->sum('money');
                    $un_pay = BillModel::where('user_id', $this->auth->id)
                        ->where('type', 1)
                        ->where('year', $item->year)
                        ->where('status', 1)
                        ->sum('money');

                    $item->pay = $pay;
                    $item->un_pay = $un_pay;
                    return $item;
                });
            $result = array("total" => $list->total(), "rows" => $list->items());
            return json($result);
        }
        return $this->view->fetch();
    }

    public function month()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = BillModel::where('user_id', $this->auth->id)
                ->where('type', 1)
                ->whereIn('status', [1, 2])
                ->field('month,sum(money) as total_money')
                ->group('month')
                ->paginate($limit)->each(function ($item) {
                    $pay = BillModel::where('user_id', $this->auth->id)
                        ->where('type', 1)
                        ->where('month', $item->month)
                        ->where('status', 2)
                        ->sum('money');
                    $un_pay = BillModel::where('user_id', $this->auth->id)
                        ->where('type', 1)
                        ->where('month', $item->month)
                        ->where('status', 1)
                        ->sum('money');

                    $item->pay = $pay;
                    $item->un_pay = $un_pay;
                    return $item;
                });
            $result = array("total" => $list->total(), "rows" => $list->items());
            return json($result);
        }
        return $this->view->fetch();
    }


    public function day()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = BillModel::where('user_id', $this->auth->id)
                ->where('type', 1)
                ->whereIn('status', [1, 2])
                ->field('day,sum(money) as total_money')
                ->group('month')
                ->paginate($limit)->each(function ($item) {
                    $pay = BillModel::where('user_id', $this->auth->id)
                        ->where('type', 1)
                        ->where('day', $item->day)
                        ->where('status', 2)
                        ->sum('money');
                    $un_pay = BillModel::where('user_id', $this->auth->id)
                        ->where('type', 1)
                        ->where('day', $item->day)
                        ->where('status', 1)
                        ->sum('money');

                    $item->pay = $pay;
                    $item->un_pay = $un_pay;
                    return $item;
                });
            $result = array("total" => $list->total(), "rows" => $list->items());
            return json($result);
        }
        return $this->view->fetch();
    }

    public function shuidian()
    {
        //当前是否为关联查询
//        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            $where2 = [];
            // 获取前端传递的filter参数并解析
            $filter = $this->request->param('filter', '{}');
            $filterArr = json_decode($filter, true);
            if (is_array($filterArr) && isset($filterArr['type']) && $filterArr['type'] !== '') {
                // 将type条件添加到查询
                $where2['type'] = $filterArr['type'];
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $build_ids = BuildingModel::getUserBuildingIds($this->auth->id);
            $room_ids = BuildingRoomModel::whereIn('build_id', $build_ids)->column('id');
            $where2['room_id'] = ['in', $room_ids];
//            if (isset($type) && $type != '') {
//                $where2['type'] = $type;
//            }
            $list = RoomBoardLogModel::where($where2)
                ->field('name,month,type,sum(use_num) as total_use_num,sum(money) as total_money')
                ->group('month')
                ->group('type')
//                ->order($sort, $order)
                ->paginate($limit);
            $result = array("total" => $list->total(), "rows" => $list->items());
            return json($result);
        }
        return $this->view->fetch();
    }

    public function project()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $build_ids = BuildingModel::getUserBuildingIds($this->auth->id);
            $where2 = [];
            $where2['contract_id'] = ['in', function ($query) use ($build_ids) {
                $query->name('user_contract')->whereIn('build_id', $build_ids)->field('contract_id');
            }];
            $where2['status'] = 2;
            $list = BillModel::where($where2)
//                ->where('user_id', $this->auth->user_id)
                ->field('month,category,sum(money) as total_money')
                ->group('month,category')
                ->paginate($limit);
            $result = array("total" => $list->total(), "rows" => $list->items());
            return json($result);
        }
        return $this->view->fetch();
    }

    public function yajin()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            $where2 =  [];
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $build_ids = BuildingModel::getUserBuildingIds($this->auth->id);
            $where2['build_id'] = ['in', $build_ids];
            $where2['b.type'] = 2;
            $list = BillModel::alias('b')
                ->join('user_contract c', 'c.id = b.contract_id', 'left')
                ->where($where2)
                ->where('b.user_id',$this->auth->id)
//                ->order($sort, $way)
                ->field('b.id,b.expire_date,b.remark,b.money,c.build_address,c.room_name')
                ->paginate($limit);
            $result = array("total" => $list->total(), "rows" => $list->items());
            return json($result);
        }
        return $this->view->fetch();
    }



}
